package servlet;

import dao.ProductDAO;
import pojo.Product;
import utils.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends BaseServlet {

    // 添加商品
    public void addProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        String img = req.getParameter("img");

        Product product = new Product();
        product.setName(name);
        product.setDescription(description);
        product.setImg(img);

        try {
            ProductDAO.addProduct(product);
            sendJsonResponse(resp, new ApiResult(200, "商品添加成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "商品添加失败", e.getMessage()));
        }
    }

    // 更新商品信息
    public void updateProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        String img = req.getParameter("img");

        Product product = new Product();
        product.setId(id);
        product.setName(name);
        product.setDescription(description);
        product.setImg(img);

        try {
            ProductDAO.updateProduct(product);
            sendJsonResponse(resp, new ApiResult(200, "商品更新成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "商品更新失败", e.getMessage()));
        }
    }

    // 删除商品
    public void deleteProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));

        try {
            ProductDAO.deleteProduct(id);
            sendJsonResponse(resp, new ApiResult(200, "商品删除成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "商品删除失败", e.getMessage()));
        }
    }

    // 获取所有商品
    public void getAllProducts(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Product> products = ProductDAO.getAllProducts();
            sendJsonResponse(resp, new ApiResult(200, "获取所有商品成功", products));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取所有商品失败", e.getMessage()));
        }
    }

    // 根据ID获取商品
    public void getProductById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));

        try {
            Product product = ProductDAO.getProductById(id);
            if (product != null) {
                sendJsonResponse(resp, new ApiResult(200, "获取商品成功", product));
            } else {
                sendJsonResponse(resp, new ApiResult(404, "商品不存在", null));
            }
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取商品失败", e.getMessage()));
        }
    }
}
